//
// Created by guanzhe on 16/1/4.
// Copyright (c) 2016 BMProject. All rights reserved.
//

#import "NumPoolEditVc.h"
#import "DataModel.h"
#import "UIColor+Extension.h"
#import "UIViewController+common.h"
#import "cardChooseView.h"

@interface NumPoolEditVc () <UITextFieldDelegate, cardChooseViewDelegate> {
    cardChooseView *_cardChooseView;
    NSMutableArray *_poolManageArr;
    NSString *_num;//搜索用的渠道号 一开始为空

}
@property(strong, nonatomic) IBOutlet UIView *bgView;
@property(weak, nonatomic) IBOutlet UIView *oneView;
@property(weak, nonatomic) IBOutlet UIView *twoView;
@property(weak, nonatomic) IBOutlet UITextField *manageTf;
@property(weak, nonatomic) IBOutlet UITextField *cardNumTf;
@end

@implementation NumPoolEditVc {

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bgView.layer.cornerRadius = 5.0;
    self.oneView.layer.cornerRadius = 5.0;
    self.twoView.layer.cornerRadius = 5.0;
    self.oneView.layer.borderWidth = 1;
    self.oneView.layer.borderColor = [UIColor hexValue:0x999999].CGColor;
    self.twoView.layer.borderWidth = 1;
    self.twoView.layer.borderColor = [UIColor hexValue:0x999999].CGColor;
    self.manageTf.placeholder = @"--请选择--";
    self.manageTf.delegate = self;
    if (_type == POOL_EDIT_TYPE) {
        NAV_INIT(self, @"号池修改", @"back", @selector(backButtonClick:), @"提交", @selector(rightBtnClick));
        self.manageTf.text = self.model.servicer;
        self.cardNumTf.text = self.model.mobile;
        [UserServices getNumPoolEditByToken:[[AppDelegate appDelegate] getUserInfo].token
                                     poolId:self.model.id
                            completionBlock:^(int result, id responseObject) {
                                CommonModel *commonModel = responseObject;
                                _poolManageArr = commonModel.serList;
                                [self initChooseView];
                            }];
    } else {
        NAV_INIT(self, @"号池添加", @"back", @selector(backButtonClick:), @"提交", @selector(rightBtnClick));
        self.manageTf.text = @"";
        self.cardNumTf.text = @"";
        [UserServices getNumPoolAddByToken:[[AppDelegate appDelegate] getUserInfo].token
                           completionBlock:^(int result, id responseObject) {
                               CommonModel *commonModel = responseObject;
                               _poolManageArr = commonModel.serList;
                               [self initChooseView];
                           }];
    }
}

- (void)rightBtnClick {
    if (IsStringEmpty(self.manageTf.text)) {
        [SVProgressHUD showErrorWithStatus:@"渠道经理不能为空"];
        return;
    }
    if (IsStringEmpty(self.cardNumTf.text)) {
        [SVProgressHUD showErrorWithStatus:@"卡号不能为空"];
        return;
    }
    if (_type == POOL_EDIT_TYPE) {
        [UserServices editNumPoolByMobile:self.cardNumTf.text
                                 servicer:_num
                                    token:[[AppDelegate appDelegate] getUserInfo].token
                                   poolId:self.model.id
                          completionBlock:^(int result, id responseObject) {
                              [SVProgressHUD showSuccessWithStatus:@"修改成功"];
                              [self backButtonClick:nil];
                          }];
    } else {
        [UserServices addNumPoolByMobile:self.cardNumTf.text
                                servicer:_num
                                   token:[[AppDelegate appDelegate] getUserInfo].token
                         completionBlock:^(int result, id responseObject) {
                             [SVProgressHUD showSuccessWithStatus:@"添加成功"];
                             [self backButtonClick:nil];
                         }];
    }

}


- (void)initChooseView {
    _cardChooseView = [[[NSBundle mainBundle] loadNibNamed:@"cardChooseView" owner:nil options:nil] lastObject];
    _cardChooseView.delegate = self;
    NSMutableArray *cardList = [NSMutableArray array];
    for (DitchManageModel *ditchManageModel in _poolManageArr) {
        [cardList addObject:[SelectTypeBean beanWithId:ditchManageModel.servicerCode name:[ditchManageModel.userName stringByAppendingString:[[@"(" stringByAppendingString:[NSString ignoreEmptyStr:ditchManageModel.servicerCode]] stringByAppendingString:@")"]]]];
    }
    _cardChooseView.cardList = cardList;
    self.manageTf.inputView = _cardChooseView;
}

- (void)cancel {
    [self.view endEditing:YES];
}

- (void)chooseCard:(SelectTypeBean *)cardInfo {
    [self.view endEditing:YES];
    self.manageTf.text = cardInfo.name;
    _num = cardInfo.id;//放置渠道id
}

- (void)cardChooseView:(UIPickerView *)cardChooseView didSelectRow:(NSInteger)row withCardBean:(SelectTypeBean *)cardInfo {

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (toBeString.length > 0 && range.length != 1) { //最大0个字符
        textField.text = [toBeString substringToIndex:0];
        return NO;
    }
    if ([string chenkInputNSCharacterSetWithType:6])//kEmpty
    {
        return YES;
    }
    else
        return NO;
}


@end